package com.pureut.equipment.service.impl;

import com.alibaba.fastjson2.JSON;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.pureut.common.core.exception.GlobalException;
import com.pureut.common.core.web.domain.AjaxResult;
import com.pureut.common.datascope.annotation.DataScope;
import com.pureut.common.security.utils.DictUtils;
import com.pureut.common.security.utils.SecurityUtils;
import com.pureut.equipment.domain.MaintenanceProject;
import com.pureut.equipment.domain.MaintenanceWorkOrder;
import com.pureut.equipment.domain.MaintenanceWorkOrderProject;
import com.pureut.equipment.domain.dto.*;
import com.pureut.equipment.domain.vo.AuditVo;
import com.pureut.equipment.domain.vo.MaintenanceWorkOrderProjectVo;
import com.pureut.equipment.domain.vo.MaintenanceWorkOrderVo;
import com.pureut.equipment.mapper.MaintenanceProjectMapper;
import com.pureut.equipment.mapper.MaintenanceWorkOrderMapper;
import com.pureut.equipment.mapper.MaintenanceWorkOrderProjectMapper;
import com.pureut.equipment.service.MaintenanceWorkOrderProjectService;
import com.pureut.equipment.service.MaintenanceWorkOrderService;
import com.pureut.equipment.util.TimeUtils;
import com.pureut.production.api.FeignProductionService;
import com.pureut.system.api.FeignService;
import com.pureut.system.api.domain.SysDictData;
import com.pureut.system.api.domain.SysUser;
import com.pureut.system.api.vo.SysAuditHistoryVo;
import com.pureut.system.api.vo.SysWorkbenchAuditAgentVo;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.*;
import java.util.stream.Collectors;

/***
 *
 * Author:保养工单实现层
 * Date:2023/04/24 9:49
 * @DESC
 */
@Service
public class MaintenanceWorkOrderServiceImpl extends ServiceImpl<MaintenanceWorkOrderMapper, MaintenanceWorkOrder> implements MaintenanceWorkOrderService {

    /**
     * 保养工单mapper
     */
    @Resource
    MaintenanceWorkOrderMapper maintenanceWorkOrderMapper;

    /**
     * 基础模块fegin
     */
    @Resource
    FeignService feignService;

    /**
     * 保养工单-保养项目service
     */
    @Resource
    MaintenanceWorkOrderProjectService maintenanceWorkOrderProjectService;

    /**
     * 保养工单-保养项目mapper
     */
    @Resource
    MaintenanceWorkOrderProjectMapper maintenanceWorkOrderProjectMapper;

    /**
     * 生产模块fegin
     */
    @Resource
    FeignProductionService feignProductionService;

    /**
     * 保养项目mapper
     */
    @Resource
    MaintenanceProjectMapper maintenanceProjectMapper;

    /**
     * 保养工单列表
     *
     * @param maintenanceWorkOrderVo
     * @return
     */
    @Override
    @DataScope(deptAlias = "a")
    public List<MaintenanceWorkOrderDto> getList(MaintenanceWorkOrderVo maintenanceWorkOrderVo) {

        List<MaintenanceWorkOrderDto> workOrder = maintenanceWorkOrderMapper.getWorkOrder(maintenanceWorkOrderVo);

        //获取生成方式字典
        List<SysDictData> generationModeArray = DictUtils.getDictCache("maintenance_work_order_generation_mode");
        Map<String, String> generationModeMap = generationModeArray.stream().collect(Collectors.toMap(SysDictData::getDictValue, SysDictData::getDictLabel));

        //获取状态字典
        List<SysDictData> statusArray = DictUtils.getDictCache("maintenance_work_order_status");
        Map<String, String> statusMap = statusArray.stream().collect(Collectors.toMap(SysDictData::getDictValue, SysDictData::getDictLabel));
        for (MaintenanceWorkOrderDto entity : workOrder) {
            entity.setStatusDict(statusMap.get(entity.getStatus()));
            entity.setGenerationModeDict(generationModeMap.get(entity.getGenerationMode()));
            if (entity.getMaintenanceStartTime() != null && entity.getMaintenanceEndTime() != null) {
                entity.setMaintenanceWorkHour(TimeUtils.getDifferHour(entity.getMaintenanceStartTime(), entity.getMaintenanceEndTime()));
            }
        }
        return workOrder;
    }

    /**
     * 保养工单新增
     *
     * @param maintenanceWorkOrderVo
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean add(MaintenanceWorkOrderVo maintenanceWorkOrderVo) throws Exception {
        MaintenanceWorkOrder maintenanceWorkOrder = new MaintenanceWorkOrder();
        String authorityCoding = feignService.getAuthorityCoding("device:maintenance:workorder:list");
        if (authorityCoding == null) {
            throw new GlobalException("该单据未进行配置，无法生成单据编码");
        }
        maintenanceWorkOrder.setMaintenanceNumber(authorityCoding)
                .setCreateBy(SecurityUtils.getUsername())
                .setCreateTime(new Date())
                .setDeptId(SecurityUtils.getDeptId())
                .setStatus(1)
                .setGenerationMode(1)
                .setMaintenanceProjectIdStr(maintenanceWorkOrderVo.getMaintenanceProjectIdStr())
                .setDeviceId(maintenanceWorkOrderVo.getDeviceId());
        return save(maintenanceWorkOrder);
    }

    /**
     * 保养工单新增(当日生成调用)
     */
    @Override
    public boolean addToday(MaintenanceWorkOrderVo maintenanceWorkOrderVo) throws Exception {
        List<String> deviceIdList = Arrays.asList(maintenanceWorkOrderVo.getDeviceIds().split(","));
        List<MaintenanceWorkOrder> maintenanceWorkOrderList = new ArrayList<>();
        for (String entity : deviceIdList) {
            MaintenanceWorkOrder maintenanceWorkOrder = new MaintenanceWorkOrder();
            String authorityCoding = feignService.getAuthorityCoding("device:maintenance:workorder:list");
            if (authorityCoding == null) {
                throw new GlobalException("该单据未进行配置，无法生成单据编码");
            }
            maintenanceWorkOrder.setMaintenanceNumber(authorityCoding)
                    .setCreateBy(SecurityUtils.getUsername())
                    .setCreateTime(new Date())
                    .setDeptId(SecurityUtils.getDeptId())
                    .setStatus(1)
                    .setSubordinatePlan(maintenanceWorkOrderVo.getSubordinatePlan())
                    .setGenerationMode(2)
                    .setMaintenanceProjectIdStr(maintenanceWorkOrderVo.getMaintenanceProjectIdStr())
                    .setDeviceId(Long.parseLong(entity));
            maintenanceWorkOrderList.add(maintenanceWorkOrder);
        }
        return saveBatch(maintenanceWorkOrderList);
    }

    /**
     * 派工
     *
     * @param maintenanceWorkOrderVo
     * @return
     */
    @Override
    public boolean dispatch(MaintenanceWorkOrderVo maintenanceWorkOrderVo) {
        MaintenanceWorkOrder maintenanceWorkOrder = getById(maintenanceWorkOrderVo.getId());
        String maintenanceProjectIdStr = maintenanceWorkOrderVo.getMaintenancePeopleIdStr();
        //获取保养人数
        List<String> peopleIdList = Arrays.asList(maintenanceWorkOrderVo.getMaintenancePeopleIdStr().split(","));
        maintenanceWorkOrder.setGroupCode(maintenanceWorkOrderVo.getGroupCode())
                .setMaintenancePeopleIdStr(maintenanceProjectIdStr)
                .setStatus(2)
                .setMaintenanceNumberPeople(peopleIdList.size());
        return updateById(maintenanceWorkOrder);
    }

    /**
     * 执行保养
     *
     * @param maintenanceWorkOrderVo
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean execute(MaintenanceWorkOrderVo maintenanceWorkOrderVo) {

        MaintenanceWorkOrder maintenanceWorkOrder = getById(maintenanceWorkOrderVo.getId());
        //只有被派工的对应人员才能进行执行操作
        Long userId = SecurityUtils.getUserId();
        List<String> peopleIdList = Arrays.asList(maintenanceWorkOrder.getMaintenancePeopleIdStr().split(","));

        boolean contains = peopleIdList.contains(String.valueOf(userId));
        //获取班组组长id
        String groupCode = maintenanceWorkOrder.getGroupCode();
        AjaxResult dataByGroupCode = feignProductionService.getDataByGroupCode(groupCode);
        GroupDataDto data = JSON.parseObject(JSON.toJSONString(dataByGroupCode.get("data")), GroupDataDto.class);
        long groupMonitor = data.getGroupMonitor();

        if (!contains && userId != groupMonitor) {
            throw new GlobalException("当前登陆人员不属于该单据的班组组长或保养人员");
        }
        maintenanceWorkOrder.setMaintenanceStartTime(maintenanceWorkOrderVo.getMaintenanceStartTime())
                .setMaintenanceEndTime(maintenanceWorkOrderVo.getMaintenanceEndTime())
                .setSparePartsIdStr(maintenanceWorkOrderVo.getSparePartsIdStr())
                .setStatus(3);
        boolean b = updateById(maintenanceWorkOrder);
        if (b) {
            List<MaintenanceWorkOrderProjectVo> maintenanceWorkOrderProjectList = maintenanceWorkOrderVo.getMaintenanceWorkOrderProjectList();
            List<MaintenanceWorkOrderProject> result = new ArrayList<>();
            for (MaintenanceWorkOrderProjectVo entity : maintenanceWorkOrderProjectList) {
                MaintenanceWorkOrderProject maintenanceWorkOrderProject = new MaintenanceWorkOrderProject();
                maintenanceWorkOrderProject.setMaintenanceProjectId(entity.getId())
                        .setOperator(SecurityUtils.getUsername())
                        .setOperation(entity.getOperation())
                        .setRemark(entity.getRemark())
                        .setWorkOrderId(maintenanceWorkOrder.getId());
                result.add(maintenanceWorkOrderProject);
            }
            maintenanceWorkOrderProjectService.saveBatch(result);
        }
        return b;
    }

    /**
     * 提交审核
     *
     * @param id
     * @return
     */
    @Override
    public boolean audit(Long id) {
        MaintenanceWorkOrder maintenanceWorkOrder = getById(id);
        maintenanceWorkOrder.setStatus(4);

        //新增审核代办
        SysWorkbenchAuditAgentVo sysWorkbenchAuditAgentVo = new SysWorkbenchAuditAgentVo();

        sysWorkbenchAuditAgentVo.setDocConfig("device:maintenance:workorder:examine");
        sysWorkbenchAuditAgentVo.setReceiptName("保养工单");
        sysWorkbenchAuditAgentVo.setReceiptNumber(maintenanceWorkOrder.getMaintenanceNumber());//
        sysWorkbenchAuditAgentVo.setStatus(1);//待审核
        sysWorkbenchAuditAgentVo.setReceiptContent("有单据待审核,请及时审核");

        feignService.insertAuditAgent(sysWorkbenchAuditAgentVo);

        return updateById(maintenanceWorkOrder);
    }

    /**
     * 撤销审核
     *
     * @param id
     * @return
     */
    @Override
    public boolean revocation(Long id) {
        MaintenanceWorkOrder maintenanceWorkOrder = getById(id);
        maintenanceWorkOrder.setStatus(3);

        feignService.deleteDoc(maintenanceWorkOrder.getMaintenanceNumber(),"保养工单");

        return updateById(maintenanceWorkOrder);
    }

    /**
     * 单据审核
     *
     * @param auditVo
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean documentAudit(AuditVo auditVo) {
        MaintenanceWorkOrder maintenanceWorkOrder = getById(auditVo.getId());
        if (maintenanceWorkOrder.getStatus() == 4 || maintenanceWorkOrder.getStatus() == 6) {
            String perms = "device:maintenance:workorder:examine";
            //获取菜单id
            int isAdopt = auditVo.getAuditResult();
            String auditDoc = maintenanceWorkOrder.getMaintenanceNumber();
            String auditRemarks = auditVo.getAuditOpinion();
            Long userId = SecurityUtils.getUserId();
            AjaxResult sequence = feignService.getSequenceTwo(perms, 11, isAdopt, auditRemarks, auditDoc, userId);
            Integer data = JSON.parseObject(JSON.toJSONString(sequence.get("data")), Integer.class);
            if (data == null) {
                throw new GlobalException("未对该审核进行配置");
            }
            //1:通过，2：审核中，3：没权限 4:该人已经审核过
            if (auditVo.getAuditResult() == 1) {
                if (data == 1) {
                    maintenanceWorkOrder.setStatus(7);

                    feignService.updateStatus(3,maintenanceWorkOrder.getMaintenanceNumber(),"保养工单");

                    return updateById(maintenanceWorkOrder);
                } else if (data == 2) {
                    maintenanceWorkOrder.setStatus(6);
                    return updateById(maintenanceWorkOrder);
                } else if (data == 3) {
                    throw new GlobalException("没有审核权限");
                } else {
                    throw new GlobalException("该用户已经审核过");
                }
            } else {
                if (data == 1 || data == 2) {
                    maintenanceWorkOrder.setStatus(5);

                    //新增审核代办
                    SysWorkbenchAuditAgentVo sysWorkbenchAuditAgentVo = new SysWorkbenchAuditAgentVo();

                    sysWorkbenchAuditAgentVo.setDocConfig("device:maintenance:workorder:examine");
                    sysWorkbenchAuditAgentVo.setReceiptName("保养工单");
                    sysWorkbenchAuditAgentVo.setReceiptNumber(maintenanceWorkOrder.getMaintenanceNumber());//
                    sysWorkbenchAuditAgentVo.setStatus(2);//待处理
                    sysWorkbenchAuditAgentVo.setReceiptContent("有单据未通过,请及时查看");

                    sysWorkbenchAuditAgentVo.setPassFlag("1");
                    sysWorkbenchAuditAgentVo.setCreateName(maintenanceWorkOrder.getCreateBy());

                    feignService.insertAuditAgent(sysWorkbenchAuditAgentVo);

                    return updateById(maintenanceWorkOrder);
                } else if (data == 3) {
                    throw new GlobalException("没有审核权限");
                } else {
                    throw new GlobalException("该用户已经审核过");
                }
            }
        } else {
            throw new GlobalException("只能对待审核和审核中的数据进行处理");
        }
    }

    /**
     * 修改
     *
     * @param maintenanceWorkOrderVo
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean updateWorkOrder(MaintenanceWorkOrderVo maintenanceWorkOrderVo) {

        MaintenanceWorkOrder maintenanceWorkOrder = getById(maintenanceWorkOrderVo.getId());
        if (maintenanceWorkOrder.getStatus() == 5) {
            List<String> peopleIds = Arrays.asList(maintenanceWorkOrderVo.getMaintenancePeopleIdStr().split(","));
            maintenanceWorkOrder.setDeviceId(maintenanceWorkOrderVo.getDeviceId())
                    .setMaintenancePeopleIdStr(maintenanceWorkOrderVo.getMaintenancePeopleIdStr())
                    .setMaintenanceNumberPeople(peopleIds.size())
                    .setMaintenanceProjectIdStr(maintenanceWorkOrderVo.getMaintenanceProjectIdStr())
                    .setGroupCode(maintenanceWorkOrderVo.getGroupCode())
                    .setMaintenanceStartTime(maintenanceWorkOrderVo.getMaintenanceStartTime())
                    .setMaintenanceEndTime(maintenanceWorkOrderVo.getMaintenanceEndTime());
            //调用审核
            SysAuditHistoryVo sysAuditHistoryVo = new SysAuditHistoryVo();
            String perms = "device:maintenance:workorder:examine";
            String auditDoc = maintenanceWorkOrder.getMaintenanceNumber();
            sysAuditHistoryVo.setAuditDoc(auditDoc);
            sysAuditHistoryVo.setPerms(perms);
            AjaxResult history = feignService.editHistory(sysAuditHistoryVo);
            String data = JSON.parseObject(JSON.toJSONString(history.get("msg")), String.class);
            if ("操作成功".equals(data)) {

                //修改审核代办状态为已处理
                feignService.updateStatus(3,maintenanceWorkOrder.getMaintenanceNumber(),"保养工单");//
                maintenanceWorkOrder.setStatus(3);
            } else {
                return false;
            }
            updateById(maintenanceWorkOrder);
            //获取该保养工单下之前的保养工单-保养项目数据并删除
            List<MaintenanceWorkOrderProject> maintenanceWorkOrderProjects = maintenanceWorkOrderProjectMapper.selectList(new QueryWrapper<MaintenanceWorkOrderProject>().lambda().eq(MaintenanceWorkOrderProject::getWorkOrderId, maintenanceWorkOrder.getId()));
            if (maintenanceWorkOrderProjects.size() > 0) {
                maintenanceWorkOrderProjectMapper.deleteBatchIds(maintenanceWorkOrderProjects);
            }
            List<MaintenanceWorkOrderProjectVo> maintenanceWorkOrderProjectList = maintenanceWorkOrderVo.getWorkOrderProjectList();
            List<MaintenanceWorkOrderProject> result = new ArrayList<>();
            for (MaintenanceWorkOrderProjectVo entity : maintenanceWorkOrderProjectList) {
                MaintenanceWorkOrderProject maintenanceWorkOrderProject = new MaintenanceWorkOrderProject();
                maintenanceWorkOrderProject.setMaintenanceProjectId(entity.getId())
                        .setOperator(SecurityUtils.getUsername())
                        .setOperation(entity.getOperation())
                        .setRemark(entity.getRemark())
                        .setWorkOrderId(maintenanceWorkOrder.getId());
                result.add(maintenanceWorkOrderProject);
            }
            return maintenanceWorkOrderProjectService.saveBatch(result);
        } else {
            throw new GlobalException("只能修改未通过的单据");
        }
    }

    /**
     * 查看
     *
     * @param id
     * @return
     */
    @Override
    public MaintenanceWorkOrderDto getView(Long id) {
        MaintenanceWorkOrderDto maintenanceWorkOrderDto = maintenanceWorkOrderMapper.getWorkOrderById(id);
        //通过线别编码获取线别名称
        String lineCode = maintenanceWorkOrderDto.getInstallationLocation();
        AjaxResult modelingLineInfo = feignService.getModelingLineInfo(lineCode);
        SysLineDto sysLineDto = JSON.parseObject(JSON.toJSONString(modelingLineInfo.get("data")), SysLineDto.class);
        maintenanceWorkOrderDto.setLineName(sysLineDto.getLineName());
        //获取保养级别字典
        List<SysDictData> levelArray = DictUtils.getDictCache("maintenance_project_level");
        Map<String, String> levelMap = levelArray.stream().collect(Collectors.toMap(SysDictData::getDictValue, SysDictData::getDictLabel));
        //班组数据
        String groupCode = maintenanceWorkOrderDto.getGroupCode();
        if (groupCode != null) {
            AjaxResult dataByGroupCode = feignProductionService.getDataByGroupCode(groupCode);
            GroupDataDto data = JSON.parseObject(JSON.toJSONString(dataByGroupCode.get("data")), GroupDataDto.class);
            maintenanceWorkOrderDto.setGroupName(data.getGroupName());
            maintenanceWorkOrderDto.setGroupMonitorName(data.getGroupMonitorName());
            maintenanceWorkOrderDto.setGroupMonitor(data.getGroupMonitor());
            //通过组员id获取组员名称
            String userIds = maintenanceWorkOrderDto.getMaintenancePeopleIdStr();

            AjaxResult userNameByIds = feignService.getUserNameByIds(userIds);
            List<SysUser> data1 = JSON.parseArray(JSON.toJSONString(userNameByIds.get("data")), SysUser.class);
            List<GroupMemberDto> groupMemberList = new ArrayList<>();
            for (SysUser entity : data1) {
                GroupMemberDto groupMemberDto = new GroupMemberDto();
                groupMemberDto.setId(entity.getUserId());
                groupMemberDto.setName(entity.getUserName());
                groupMemberList.add(groupMemberDto);
            }
            maintenanceWorkOrderDto.setGroupMemberList(groupMemberList);
        }
        //获取保养工单-保养项目数据
        List<MaintenanceWorkOrderProject> maintenanceWorkOrderProjects = maintenanceWorkOrderProjectMapper.selectList(new QueryWrapper<MaintenanceWorkOrderProject>().lambda().eq(MaintenanceWorkOrderProject::getWorkOrderId, maintenanceWorkOrderDto.getId()));
        if (maintenanceWorkOrderProjects.size() > 0) {
            //根据id获取保养项目列表
            List<String> projectIdStr = Arrays.asList(maintenanceWorkOrderDto.getMaintenanceProjectIdStr().split(","));
            List<MaintenanceWorkOrderProjectDto> result = new ArrayList<>();
            List<MaintenanceProject> maintenanceProjects = maintenanceProjectMapper.selectList(new QueryWrapper<MaintenanceProject>().lambda().in(MaintenanceProject::getId, projectIdStr));
            for (MaintenanceWorkOrderProject entity : maintenanceWorkOrderProjects) {
                for (MaintenanceProject maintenanceProject : maintenanceProjects) {
                    if (entity.getMaintenanceProjectId() == maintenanceProject.getId()) {
                        MaintenanceWorkOrderProjectDto maintenanceWorkOrderProjectDto = new MaintenanceWorkOrderProjectDto();

                        maintenanceWorkOrderProjectDto.setId(maintenanceProject.getId());
                        maintenanceWorkOrderProjectDto.setMaintenanceProjectCode(maintenanceProject.getMaintenanceProjectCode());
                        maintenanceWorkOrderProjectDto.setMaintenanceProjectName(maintenanceProject.getMaintenanceProjectName());
                        maintenanceWorkOrderProjectDto.setMaintenancePosition(maintenanceProject.getMaintenancePosition());
                        maintenanceWorkOrderProjectDto.setMaintenanceLevel(String.valueOf(maintenanceProject.getMaintenanceLevel()));
                        maintenanceWorkOrderProjectDto.setMaintenanceLevelDict(levelMap.get(String.valueOf(maintenanceProject.getMaintenanceLevel())));
                        maintenanceWorkOrderProjectDto.setMaintenanceContent(maintenanceProject.getMaintenanceContent());
                        maintenanceWorkOrderProjectDto.setWorkingHour(maintenanceProject.getWorkingHour());
                        maintenanceWorkOrderProjectDto.setRemark(entity.getRemark());
                        maintenanceWorkOrderProjectDto.setOperation(String.valueOf(entity.getOperation()));
                        maintenanceWorkOrderProjectDto.setOperator(entity.getOperator());
                        maintenanceWorkOrderProjectDto.setCreateBy(maintenanceProject.getCreateBy());

                        result.add(maintenanceWorkOrderProjectDto);
                    }
                }
            }
            maintenanceWorkOrderDto.setWorkOrderProjectList(result);
        } else {
            List<String> projects = Arrays.asList(maintenanceWorkOrderDto.getMaintenanceProjectIdStr().split(","));
            List<MaintenanceProjectDto> maintenanceProjectDtos = maintenanceProjectMapper.selectAll(projects);
            maintenanceWorkOrderDto.setMaintenanceProjectList(maintenanceProjectDtos);
        }

        return maintenanceWorkOrderDto;
    }

    /**
     * 删除
     *
     * @param id
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean deleteWorkOrder(Long id) {
        MaintenanceWorkOrder byId = getById(id);
        if (byId.getStatus() == 1) {
            feignService.deleteDoc(byId.getMaintenanceNumber(),"保养工单");
            return removeById(id);
        } else {
            throw new GlobalException("只能删除待派工的单据");
        }
    }

    /**
     * 保养工单新增(定时任务调用)
     *
     * @param maintenanceWorkOrderVo
     * @return
     */
    @Override
    public boolean addScheduling(MaintenanceWorkOrderVo maintenanceWorkOrderVo) throws Exception {
        List<String> deviceList = Arrays.asList(maintenanceWorkOrderVo.getDeviceIds().split(","));
        List<MaintenanceWorkOrder> result = new ArrayList<>();
        for (String entity : deviceList) {
            MaintenanceWorkOrder maintenanceWorkOrder = new MaintenanceWorkOrder();
            maintenanceWorkOrder.setMaintenanceNumber(feignService.splitList("BYGD"))
                    .setSubordinatePlan(maintenanceWorkOrderVo.getSubordinatePlan())
                    .setCreateBy(SecurityUtils.getUsername())
                    .setCreateTime(new Date())
                    .setDeptId(SecurityUtils.getDeptId())
                    .setStatus(1)
                    .setGenerationMode(2)
                    .setMaintenanceProjectIdStr(maintenanceWorkOrderVo.getMaintenanceProjectIdStr())
                    .setDeviceId(Long.parseLong(entity));
            result.add(maintenanceWorkOrder);
        }
        return saveBatch(result);
    }
}
